<template>
  <a-modal
    style="width: 50%"
    :title="titleMap[mode]"
    :visible="visible"
    :confirm-loading="confirmLoading"
    :destroyOnClose="true"
    @cancel="handleCancel"
    :maskClosable="false">
    <div style="margin-top: 12px;min-height: 300px;min-width: 650px">
      <a-form-model
        ref="ruleAddForm"
        :model="addForm"
        :rules="addRules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol">
        <a-form-model-item ref="name" label="角色类型" prop="name" :style="itemStyle">
          <a-input v-model="addForm.name" type="text" placeholder="请输入角色类型"/>
        </a-form-model-item>
        <a-form-model-item label="备注" :style="itemStyle">
          <a-input v-model="addForm.description" type="text" placeholder="请输入备注"/>
        </a-form-model-item>
      </a-form-model>
    </div>
    <template slot="footer">
      <a-button key="back" @click="handleCancel">
        关闭
      </a-button>
      <a-button type="primary" :visible="visibleBtn" :loading="loadingBtn" @click="handleOk">
        保存
      </a-button>
    </template>
  </a-modal>
</template>
<script>
import { addRoleTypes, detailRoleTypes } from '@/api/base'
export default {
    components: { },
    data () {
        return {
            addRules: {
                name: [
                    { required: true, message: '请输入角色类型!', trigger: 'blur' }
                ]
            },
            addForm: {
                description: '',
                name: ''
            },
            wrapperCol: { span: 16 },
            labelCol: { span: 6 },
            iconChooseVisible: false,
            itemStyle: 'float: left;width: 50%;min-width:300px',
            model: {},
            visible: false,
            confirmLoading: false,
            loading: false,
            visibleBtn: false,
            loadingBtn: false,
            mode: 'add',
            titleMap: {
                add: '新增',
                edit: '编辑'
            },
            rowData: ''
        }
    },
    methods: {
        // 显示
        open (mode = 'add') {
            this.mode = mode
            this.visible = true
            return this
        },
        // 获取行数据和id
        setData (row) {
            this.rowData = row
            if (this.mode == 'edit') {
                detailRoleTypes(row.roleTypeId).then(res => {
                    if (res.code == 200) {
                        this.addForm = res.data
                    }
                })
            }
        },
        // 关闭
        handleCancel () {
            this.visible = false
            this.loadingBtn = false
            this.visibleBtn = false
            this.$emit('close')
        },
        // 确认
        handleOk () {
            if (this.mode == 'edit') {
                this.$notification['error']({
                    message: '提示',
                    description: '编辑接口更新中...',
                    duration: 8
                })
                return
            }
            this.loadingBtn = true
            this.visibleBtn = true
            this.$refs.ruleAddForm.validate(valid => {
                if (valid) {
                    // let Api=this.mode=='add'?addRoleTypes:editRoleTypes
                    addRoleTypes(this.addForm).then(res => {
                        if (res.code === 200) {
                            this.$notification['success']({
                                message: '提示',
                                description: '保存成功！',
                                duration: 8
                            })
                        } else {
                            this.$notification['error']({
                                message: '提示',
                                description: res.message,
                                duration: 8
                            })
                        }
                        this.visibleBtn = false
                        this.visible = false
                        this.loadingBtn = false
                        this.$emit('success')
                    })
                } else {
                    this.loadingBtn = false
                    this.visibleBtn = false
                    return false
                }
            })
        }
    }
}
</script>
<style scoped lang="less" ></style>
